<?php 

declare(strict_types=1);

namespace WanZ\App\Utils\Prometheus;

use Prometheus\Exception\MetricsRegistrationException;

/**
 * Class User
 * @package WanZ\App\Utils\Prometheus
 *
 */
class User extends Prometheus
{
    /**
     * 记录用户余额消费失败次数
     *
     * @param int $userId
     * @param int $wxBalance
     * @param int $aliBalance
     * @param int $errorNum
     */
    public function balanceErrorNumToUser(int $userId, int $wxBalance = 0, int $aliBalance = 0, int $errorNum = 1)
    {
        $platform = $operatorType = '';
        if ($wxBalance != 0) {
            $platform = 'wx';
            if ($wxBalance > 0) {
                $operatorType = $platform .'_add';
            } else {
                $operatorType = $platform .'_minus';
            }
        }

        if ($aliBalance != 0) {
            $platform = 'ali';
            if ($aliBalance > 0) {
                $operatorType = $platform .'_add';
            } else {
                $operatorType = $platform .'_minus';
            }
        }

        $labels = [
            'operator_type' => $operatorType,
            'platform' => $platform
        ];

        $this->incBy(
            'user_balance',
            "{$platform}_error_total",
            '用户余额(充值/消费)',
            array_merge($this->getDefaultLabel(), $labels),
            $errorNum
        );
    }

    /**
     * 记录充电用户注册失败次数
     *
     * @param string $wxMiniOpenId
     * @param string $platform
     * @param int $errorNum
     * @return void
     */
    public function addErrorNumToUserRegister(string $wxMiniOpenId, string $platform, int $errorNum = 1)
    {
        $labels = [
            'platform' => $platform,
        ];

        $this->incBy(
            "user_register",
            'error_total',
            '充电用户注册失败',
            array_merge($this->getDefaultLabel(), $labels),
            $errorNum
        );
    }

    /**
     * 记录 获取充电用户信息失败次数
     *
     * @param int $userId
     * @param int $errorNum
     */
    public function addErrorNumToGetUserInfo(int $userId, int $errorNum = 1)
    {
        $this->incBy(
            'get_user_info',
            'error_total',
            '获取充电用户信息失败',
            ['Log_tag' => 'getClientUserInfoErrorRes'],
            $errorNum
        );
    }

    /**
     * 登录失败报警
     *
     * @param string $type
     * @param array $labels
     * @param int $errorNum
     */
    public function loginError(string $type, array $labels = [], int $errorNum = 1)
    {
        $platform = $type == 'wx_login' ?  '微信小程序' : '支付宝小程序';

        $this->incBy(
            $type,
            'error_total',
            $platform.'登录失败结果记录',
            $labels,
            $errorNum
        );
    }

    /**
     * oauth授权失败报警
     *
     * @param string $type
     * @param array $labels
     * @param int $errorNum
     */
    public function oauthLoginError(string $type, array $labels = [], int $errorNum = 1)
    {
        $platform = $type == 'oauth_wx_login' ?  '微信小程序' : '支付宝小程序';

        $this->incBy(
            $type,
            'error_total',
            $platform.'oauth授权失败结果记录',
            $labels,
            $errorNum
        );
    }

    /**
     * 获取默认label
     * @return int[]
     */
    protected function getDefaultLabel(): array
    {
        return $this->getCompanyIdAndUType();
    }
}
